﻿using System;
using System.Data;
using ServiciosGeograficos.Persistencia;
using ServiciosGeograficos.Reconocimiento;


namespace Pgmobile.Reconocimiento
{
    public class Interprete
    {
        private static int DISTANCIA_METROS_ORIGEN = 500;
        private static int DISTANCIA_METROS_DESTINO = 500;

        public Interprete()
        {
        }

        // Dado una calle y una esquina devuelve el/los nombres de las calles interseccion.
        public DataSet intersectanCalles(String nmCalle, String nmEsquina)
        {
            InterpreteCalles interpreteCalles = new InterpreteCalles();
            return interpreteCalles.calleEsquina(nmCalle, nmEsquina);
        }

        // Dado una calle y un numero de puerta devuelve el/los nombres de las calles interseccion.
        public DataSet callePuertas(String nmCalle, String numPuerta)
        {
            InterpreteCalles interpreteCalles = new InterpreteCalles();
            return interpreteCalles.callePuertas(nmCalle, numPuerta);
        }

        // Dado el nombre (no necesariamente completo)  de una calle, devuelve todas las calles que cortan a dicha calle.
        public DataSet darEsquinas(String nmCalle)
        {
            String query = "select distinct esquina.nombre from calles calle, calles esquina where intersects(calle.the_geom, esquina.the_geom)";
            query += " and calle.nombre like '%" + nmCalle + "%'";
            query += " and calle.nombre != esquina.nombre ";
            PostgresDAO postgresDAO = new PostgresDAO();
            DataSet dsEsquinas = postgresDAO.ejecutarQuery(query, "esquinas");
            return dsEsquinas;
        }

        public DataTable darOmnibusPosibles(String xOrigen, String yOrigen, String xDestino, String yDestino)
        {
            String query = "select nombre, gid from ";
            query += "(select distance(ST_PointFromText('POINT(" + xOrigen + " " + yOrigen + ")'),linea.the_geom) as dist, linea.* ";
            query += " from lineas linea) as linea_distancia";
            query += " WHERE linea_distancia.dist <= " + DISTANCIA_METROS_ORIGEN;
            query += " and linea_distancia.gid IN ( ";
            query += " select gid from";
            query += "(select distance(ST_PointFromText('POINT(" + xDestino + " " + yDestino + ")'),linea.the_geom) as dist, linea.* ";
            query += " from lineas linea) as linea_distancia";
            query += " WHERE linea_distancia.dist <= " + DISTANCIA_METROS_DESTINO + ")";

            PostgresDAO dao = new PostgresDAO();
            DataSet dataSet = dao.ejecutarQuery(query, "lineas");
            return dataSet.Tables["lineas"];
        }

        public DataTable darParada(String x, String y, int idLinea)
        {
            String query = "select parada.id1 as id1, parada.idparada as idParada, distance(ST_PointFromText('POINT(' || parada.x || ' ' || parada.y || ')'),ST_PointFromText('POINT(" + x + " " + y + ")')) as distance, parada.x as xParada, parada.y as yParada from lineas linea, paradas parada ";
            query += "where ";
            query += "linea.id = parada.bus ";
            query += "and linea.gid = " + idLinea;
            query += "order by distance asc ";
            query += "limit 1 ";
            
            PostgresDAO dao = new PostgresDAO();
            DataSet dataSet = dao.ejecutarQuery(query, "parada");
            return dataSet.Tables["parada"];
        }
    }
}
